class Solution(object):
    def trap(self, height):
        """
        :type height: List[int]
        :rtype: int
        """
        point = 0
        water = 0
        part = []

        while point < len(height):
            if self.check(part, height[point]):
                point += 1
            else:
                water += self.compute(part)
            
        print(water)
    
    def check(self, part:list, next):
        c = len(part)
        if c == 0:
            part.append(next)
            return True
        else:
            if part[c - 1] >= next:
                part.append(next)
                return True
            else:
                part.append(next)
                return False
    
    def compute(self, part:list):
        b = part[0]
        e = part[-1]
        if b > e:
            water = (part[-1] * (len(part) - 1) - sum(part[1:]))
        else:
            water = (part[0] * (len(part) - 1) - sum(part[0:len(part) - 1]))
        part.clear()
        return water


if __name__ == "__main__":
    s = Solution()
    s.trap([0,1,0,2,1,0,1,3,2,1,2,1])
    print(s.compute([3,2,2,5]))
